/**
 * 
 */
package org.ccay.excel.meta;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.ccay.core.request.impl.RequestContext;
import org.ccay.core.util.CollectionUtil;
import org.ccay.core.util.LanguageUtil;
import org.ccay.core.util.StringUtil;
import org.ccay.registry.util.RegistryQueryUtil;
/**
 * @author chaowangbang
 *
 */
public class NamedElement {
	
	/** 名称 **/
	private String name;
	
	private String displayName;
	
	
	/**缓存语种**/
	private Map<String,String> languageNames = new HashMap<String, String>();

	/**
	 * @param name
	 */
	public NamedElement(String name) {
		this.name = name;
	}
	public Map<String, String> getLanguageNames() {
		if(languageNames.isEmpty()){
			getDisplayName();
		}
		return languageNames;
	}
	/**
	 * 获取名称。
	 * 
	 * @return
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * 判断是否当前对象 中其他显示名称包含这个值
	 * @param displayName
	 * @return
	 * Created：2013-8-31
	 * @author chaowangbang
	 */
	public boolean isCurrent(String displayName){
		if(StringUtil.isNullOrEmpty(displayName)){
			return false;
		}
		if(languageNames.isEmpty()){
			getDisplayName();
		}
		for(Entry<String, String> entry:languageNames.entrySet()){
			if(StringUtil.isNullOrEmpty(entry.getValue())){
				continue;
			}
			if(entry.getValue().trim().equalsIgnoreCase(displayName.trim())){
				return true;
			}
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((getName() == null) ? 0 : getName().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		
		if (obj == null) {
			return false;
		}
		
		if (getClass() != obj.getClass()) {
			return false;
		}
		
		return compareName(obj);
	}
	
	/**
	 * 比较name的值。
	 * 
	 * @param obj
	 * @return
	 */
	private boolean compareName(Object obj) {
		final NamedElement other = (NamedElement) obj;
		if (this.getName() == null) {
			if (other.getName() != null) {
				return false;
			}
		} else if (!this.getName().equals(other.getName())) {
			return false;
		}
		return true;
	}
	/**
	 * 获取模板生成 或者导入 导出标题的验证 书写头名称
	 * @return
	 * Created：2013-8-29
	 * @author chaowangbang
	 */
	public String getDisplayName() {
		if(StringUtil.isNullOrEmpty(this.displayName)){
			this.displayName = this.name;
		}
		
		//获取用户语种
		String userLanguage = RequestContext.getCurrent().getUserLanguage();
		String currentName = languageNames.get(userLanguage);
		if(currentName != null){
			return currentName;
		}
		//解析所有的语种
		String[] languages = RegistryQueryUtil.getValue("Ccay.GlobalParamters.ValidLanguages").split(",");
		if(CollectionUtil.isNullOrEmpty(languages)){
			currentName = LanguageUtil.getCurrenLanguageStr(this.displayName);
			languageNames.put(userLanguage, currentName);
			return currentName;
		}
		for(String str:languages){
			currentName = LanguageUtil.getLanguageStr(this.displayName, str);
			languageNames.put(str, currentName);
		}
		return languageNames.get(userLanguage);
		
	}
	public void setDisplayName(String displayName) {
		this.displayName = displayName;
		clearLanguageNames();
	}
	
	public void clearLanguageNames(){
		this.languageNames.clear();
	}
	public void setLanguageNames(Map<String, String> languageNames) {
		this.languageNames = languageNames;
	}
	
	
}
